package com.bit.frige.sub.food;

import java.util.StringTokenizer;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bit.frige.R;
import com.bit.frige.TabtestActivity;
import com.bit.frige.DB.DBContactHelper;
import com.bit.frige.DB.FoodDTO;

public class SubFoodEditFormActivity extends Activity {
	private EditText mDateDisplay;
    private EditText mDateDisplay2;
    private DBContactHelper db;
     private int mYear;
     private int mMonth;
     private int mDay;
    private int mYear2; //유통기한
    private int mMonth2;//유통기한
    private int mDay2;//유통기한
    private Button savebtn;
    private Button deletebtn;
    private String buydate;
    private String selfdate;
    private int id;
    private int cateno;
    private int place;
    private String foodname;
    static final int DATE_DIALOG_ID = 0;
    static final int DATE_DIALOG_ID2 = 1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sub_food_edit_form);
        ImageView img = (ImageView) findViewById(R.id.efoodimg);
        savebtn = (Button) findViewById(R.id.esavebtn);
        deletebtn= (Button) findViewById(R.id.edeletebtn);
        Intent intent = getIntent();
        id= intent.getIntExtra("id",0);
        db = new DBContactHelper(this);
        FoodDTO food =db.getUserInfo(id);
        foodname = food.getFoodname();
        cateno = food.getCateno();
        place = food.getPlace();
        
       
        
        TextView foodtext = (TextView) findViewById(R.id.efoodname);
        foodtext.setText(foodname);
        
        

        EditText foodnametxt = (EditText) findViewById(R.id.efoodnametxt);
        EditText grouptxt = (EditText) findViewById(R.id.egrouptxt);
        
        foodnametxt.setText(foodname);
       
        
      
        buydate = food.getBuydate().trim();
        Log.d("1","구매일자"+buydate);
		StringTokenizer stz = new StringTokenizer(buydate,"-");
		int []day = new int[3];
		for( int k=0; k<3; k++){
			day[k]= Integer.parseInt(stz.nextToken());
		}
    	mYear = day[0];
    	mMonth= day[1]-1;
    	mDay  = day[2];
        	
        	
    	selfdate = food.getSelfdate().trim();
    	Log.d("1","유통기한"+selfdate);
		stz = new StringTokenizer(selfdate,"-");
		day = new int[3];
		for( int k=0; k<3; k++){
			day[k]= Integer.parseInt(stz.nextToken());
		}
    	
    	mYear2 = day[0];
    	mMonth2= day[1]-1;
    	mDay2  = day[2];
        
        
        mDateDisplay = (EditText) findViewById(R.id.etimetext1);
        mDateDisplay2 = (EditText) findViewById(R.id.etimetext2);
        mDateDisplay.setFocusableInTouchMode(false);
        mDateDisplay2.setFocusableInTouchMode(false);
        mDateDisplay.setOnClickListener(new OnClickListener() {

                @SuppressWarnings("deprecation")
                        public void onClick(View v) {
                    showDialog(DATE_DIALOG_ID);
                   }
                  });
        mDateDisplay2.setOnClickListener(new OnClickListener() {
                  
                   @SuppressWarnings("deprecation")
                public void onClick(View v) {
                    showDialog(DATE_DIALOG_ID2);
                   }
                  });
        savebtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				  FoodDTO food = new FoodDTO();
				  food.setId(id);
                  food.setCateno(cateno);
                  food.setFoodname(foodname);
                  food.setBuydate(mDateDisplay.getText().toString());
                  food.setSelfdate(mDateDisplay2.getText().toString());
                  food.setPlace(place);
                  Log.d("업데이트값","id값: "+food.getId());
                 int re=  db.updateUserInfo(food);
                  Log.d("업데이트값","값: "+re);
                  Intent intent = new Intent(getApplicationContext(),TabtestActivity.class);
                  intent.putExtra("place",place);
//                  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  startActivity(intent);
                  finish();
				
			}
		});
       deletebtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				db.deleteUserInfo(id);
                  Intent intent = new Intent(getApplicationContext(),TabtestActivity.class);
                  intent.putExtra("place",place);
                  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  startActivity(intent);
				
			}
		});
        
        
        

        String catename ="";
        if(cateno==0){
                img.setImageResource(R.drawable.food1);
                catename="육류";
        }else if(cateno==1){
                img.setImageResource(R.drawable.food2);
                catename="생선";
        }else if(cateno==2){
                img.setImageResource(R.drawable.food3);
                catename="해산물";
        }else if(cateno==3){
                img.setImageResource(R.drawable.food4);
                catename="채소";
        }else if(cateno==4){
                img.setImageResource(R.drawable.food5);
                catename="과일";
        }else if(cateno==5){
                img.setImageResource(R.drawable.food6);
                catename="유제품";
        }else if(cateno==6){
                img.setImageResource(R.drawable.food7);
                catename="음료";
        }else if(cateno==7){
                img.setImageResource(R.drawable.food8);
                catename="기타";
        }
    	 grouptxt.setText(catename);
        	
    	// (4) 인식된 날짜를 출력
    	updateDisplay();
        	
    }
	
    	
	 protected Dialog onCreateDialog(int id) {
	          switch(id){
	          	case DATE_DIALOG_ID : return new DatePickerDialog(this,mDateSetListener,mYear,mMonth,mDay);
	          	case DATE_DIALOG_ID2 : return new DatePickerDialog(this,mDateSetListener2,mYear2,mMonth2,mDay2);
	          }
	          return null;
	         }
	
	 private DatePickerDialog.OnDateSetListener mDateSetListener = 
	         new DatePickerDialog.OnDateSetListener() {
	  
	          public void onDateSet(DatePicker view, int year, int monthOfYear,
	            int dayOfMonth) {
	           mYear = year;
	           mMonth=monthOfYear;
	           mDay=dayOfMonth;
	           // 사용자가 지정한 날짜를 출력
	           updateDisplay();
	           
	          }
	 };
	
	 private DatePickerDialog.OnDateSetListener mDateSetListener2 = new DatePickerDialog.OnDateSetListener() {
	          
	          public void onDateSet(DatePicker view, int year, int monthOfYear,
	            int dayOfMonth) {
	           mYear2 = year;
	           mMonth2=monthOfYear;
	           mDay2=dayOfMonth;
	           updateDisplay();
	           
	          }
	 };
	
	// 설정된 날짜를 TextView에 출력
	private void updateDisplay() {
	mDateDisplay.setText(
	new StringBuilder()
	.append(mYear).append("-")
	.append(mMonth+1).append("-")
	.append(mDay).append(" ")
	);
	mDateDisplay2.setText(
	       new StringBuilder()
	       //월은 시스템에서 0~11로 인식하기 때문에 1을 더해줌
	       .append(mYear2).append("-")
	       .append(mMonth2+1).append("-")
	       .append(mDay2).append(" ")
	       );
	
	}
}
